package net.expectx.pay.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * @author lijian
 */
public class LayeditTag extends TagSupport {
    /**
     * 控件唯一标志着
     */
    /**
     * 控件名称
     * 必填
     */
    private String field;
    /**
     * 别名
     * 选填
     */
    private String alias;

    /**
     * 默认值
     * 选填
     */
    private String defaultVal;
    @Override
    public int doEndTag() throws JspException {

        HttpServletRequest request=(HttpServletRequest) this.pageContext.getRequest();
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        StringBuffer buffer=new StringBuffer();
        String editName=null;
        if (null!=alias){
            editName=alias+field;
        }else{
            editName=field;
        }
        buffer.append(" <textarea\n" +
                "  name=\""+editName+"\"\n" +
                "  id=\""+editName+"\"\n" +
                "  autocomplete=\"off\"\n" +
                " >\n" +((null==defaultVal)?"":defaultVal)+
                "   </textarea>");
        buffer.append("<script>\n" +
                        "var layedit=null;\n" +
                        "    var "+editName+"Edit=null;\n"+
                "    layui.use('layedit', function(){\n" +
                "        layedit = layui.layedit;\n" +
                "        layedit.set({\n" +
                "            uploadImage: {\n" +
                "                url: '"+basePath+"manager/file/upload.action?fileType=image&catalog=image' //接口url\n" +
                "                ,type: 'GET' //默认post\n" +
                "            }\n" +
                "        });\n" +
                "        "+editName+"Edit=layedit.build('"+editName+"'); //建立编辑器\n" +
                "    });\n" +
                "function get"+editName+"Content() {\n" +
                        "        console.log("+editName+",layedit,layedit.getContent("+editName+"Edit))\n" +
                        "        return layedit.getContent("+editName+"Edit);\n" +
                        "    }\n"+
                "</script>");
        JspWriter out=this.pageContext.getOut();
        try {
            out.println(buffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return super.doEndTag();

    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getDefaultVal() {
        return defaultVal;
    }

    public void setDefaultVal(String defaultVal) {
        this.defaultVal = defaultVal;
    }
}
